/*
 * @Descripttion:
 * @Author: liuyu
 * @Date: 2023-12-04 15:44:23
 * @LastEditors: liuyu
 * @LastEditTime: 2023-12-04 15:44:23
 * @FilePath: /app/controller/role.js
 */
'use strict';

const Controller = require('egg').Controller;
class ServeController extends Controller {
  async show() {
    const { ctx: { service, helper, params } } = this;
    const res = await service.role.show({ id: params.id });
    helper.render(200, res);
  }
  async list() {
    const { ctx } = this;
    const { page = 1, page_size = 10, field, order, ...where } = ctx.query;

    const res = await ctx.service.serve.index([ 'id', 'name', 'desc', 'status', 'sort', 'createtime' ], {
      ...where,
      aid: 1,
      bid: ctx.locals.auth.userAdminBid,
    }, page, page_size, field, order);

    const total = await this.app.mysql.get('db2').count('ddwx_shop_fuwu', {
      ...where,
      aid: 1,
      bid: ctx.locals.auth.userAdminBid,
    });

    ctx.helper.render(200, {
      data: res,
      total,
    });
  }
  async create() {
    const { ctx: { service, helper, request, locals } } = this;
    const body = request.body;
    // const userInfo = await service.userAdmin.show({ id: ctx.locals.auth.userAdminBid });
    const showRes = await service.serve.show({ name: body.name, bid: locals.auth.userAdminBid });
    if (showRes) {
      helper.render(422, {}, '服务名称已存在');
      return;
    }
    try {
      await service.serve.create({
        ...body,
        aid: 1,
        bid: locals.auth.userAdminBid,
      });
      helper.render(200, {}, '添加成功');
    } catch (e) {
      helper.render(422, e, '添加失败');
    }
  }
  async update() {
    const { ctx: { service, helper, params, request } } = this;
    const body = request.body;
    const where = { id: params.id };
    try {
      await service.serve.update(body, where);
      helper.render(200, {}, '更新成功');
    } catch (e) {
      helper.render(422, e, '更新失败');
    }
  }
  async delete() {
    const { ctx: { service, helper, params, request } } = this;
    const body = request.body;
    try {
      await service.serve.delete(params.id || body.ids);
      helper.render(200, {}, '删除成功');
    } catch (e) {
      helper.render(422, e, '删除失败');
    }
  }
}

module.exports = ServeController;
